qui(
    function (require, exports, module) {
        // 整体 管理
        WrapSys.Integral = (function () {
            var node = null;

            function createBaseNode() {
                return {
                    settings: WrapSys.Dao.settings,
                    paramsData: WrapSys.Dao.getParamsData(),
                    processArr: [doProcess1, doProcess2, doProcess3, doProcess4, doProcess5,doProcess7,doProcess8, doProcess6],
                    tempNode: {}
                }
            }

            function doProcess1() {
                if (node.settings.taskid < 1) {
                    node.handler.doError(WrapSys.Config.REMIND_MSG_INFO.NO_TASK_ID_INFO);
                    return false;
                }
                if ($("span[span-mode=warn]").is(":visible")) {
                    node.handler.doError(WrapSys.Config.REMIND_MSG_INFO.TOO_LONG_TITLE_INFO);
                    return false;
                }
                if (!WrapSys.ShimManager.checkHasAllSetFrame()) {
                    WrapSys.Plugin.tips("error", WrapSys.Config.REMIND_MSG_INFO.SHIM_NEED_ACCURATE_FRAME);
                    return false;
                }
                return true;
            }

            function doProcess2() {
                WrapSys.Config.LAST_SUBMIT_TIME = WrapSys.Config.DOT_SUBMIT_TIME;
                WrapSys.Config.DOT_SUBMIT_TIME = WrapSys.Util.formatDateTimeFormat(new Date());
                var source = WrapSys.Config.PageBeanMaskEntity.getSource();
                if (!WrapSys.MaskManager.isValidDelogo(source)) {
                    WrapSys.Plugin.tips("error", WrapSys.Config.REMIND_MSG_INFO.MASK_DATA_ERROR);
                    return  false;
                }
                return true;
            }

            function doProcess3() {
                $('input[name=imageId]').val($('input[name=imageId]').val().trim());
                var buildNewPa = WrapSys.Util.buildNewPA();
                var sTime = $('input[name=sTime]').val(),
                    eTime = $('input[name=eTime]').val();
                var pic = $('input[name=imageId]').val() ? $('input[name=imageId]').val() : sTime,

                    data = {
                        '起始帧': sTime,
                        '结束帧': eTime,
                        '标题': buildNewPa.title,
                        '频道': buildNewPa.cat
                    },
                    dataSubmit = {
                        'index': node.settings.index,
                        'sTime': sTime,
                        'eTime': eTime,
                        'title': buildNewPa.title,
                        'channel': buildNewPa.cat,
                        'album': buildNewPa.album,
                        'pic': pic
                    }

                if (( "tag" + buildNewPa.cat) in WrapSys.Config.FORCE_CHECK_IMAGE) {
                    if ($('input[name=imageId]').val().length == 0) {
                        WrapSys.Plugin.tips("error", WrapSys.Config.REMIND_MSG_INFO.CHOOSE_IMAGE);
                        return false;
                    }
                }

                if (WrapSys.Util.hasImportData()) {
                    dataSubmit.importRegId = parseInt($("span[name=import-id-span]").attr("import-data-id"));
                }

                if (!WrapSys.Util.checkStAndEt(sTime, eTime)) {
                    return false;
                }
                // 填充全部 开始结束间所有数据
                VODCom.Player.splitData(sTime, eTime, false);
                if (WrapSys.Advert.checkContains([
                    {point: parseInt(sTime), errorMsg: WrapSys.Config.REMIND_MSG_INFO.ADVERT_CONTAINS_INPOINT_INFO},
                    {point: parseInt(eTime), errorMsg: WrapSys.Config.REMIND_MSG_INFO.ADVERT_CONTAINS_OUTPOINT_INFO}
                ])) {
                    return false;
                }
                var errorDataArr = WrapSys.CornerManager.findNoPositionDatas(sTime,eTime);
                if (errorDataArr.length > 0) {
                    WrapSys.Plugin.tips("error","贴片 "+errorDataArr.join(",")+ WrapSys.Config.REMIND_MSG_INFO.NEED_SET_RATIO_AND_POS);
                    return false;
                }
                if (WrapSys.Util.isAppendFragmentMode()) {
                    if (!WrapSys.Util.checkFragmentEnd(eTime)) {
                        return false;
                    }
                }
                var checkArr = [];
                checkArr.push({value: pic, error: "封面格式有误,必须为数字类型", type: "number"})
                checkArr.push({value: parseInt(pic), error: "封面不能小于起始帧", type: "ge", other: parseInt(sTime)});
                checkArr.push({value: parseInt(pic), error: "封面不能大于结束帧", type: "le", other: parseInt(eTime)})
                var numTime = $('#picNum').text();
                checkArr.push({value: parseInt(eTime), error: "结束帧不存在,不能提交", type: "lt", other: parseInt(numTime)  })
                if (!WrapSys.Plugin.Validate.batchCheck(checkArr)) {
                    console.log("interrupt checkLiveType checkRepeat")
                    return false;
                }
                if (!WrapSys.Util.validAddInfo(data)) {
                    return false;
                }
                node.tempNode.buildNewPa = buildNewPa;
                node.tempNode.dataSubmit = dataSubmit;
                node.tempNode.pic = pic;
                node.tempNode.sTime = sTime;
                node.tempNode.eTime = eTime;
                return true;
            }

            function doProcess4() {
                var dataSubmit = node.tempNode.dataSubmit
                $('#addInfo').attr('disabled', 'disabled');
                var needMark = true;
                if (WrapSys.Util.hasImportData()) {
                    var importData = WrapSys.Config.PageBeanNews.findData("regId", parseInt($("span[name=import-id-span]").attr("import-data-id")))
                    if (parseInt(dataSubmit.eTime) == importData.eTime) {
                        needMark = false;
                    }
                }
                if (needMark) {
                    var aframeData = WrapSys.AccurateFrame.getAccurateFrame();
                    if (aframeData.endSetted && aframeData.endPic == parseInt(dataSubmit.eTime)) {
                        needMark = false;
                    }
                }
                if (needMark) {
                    WrapSys.Plugin.tips("error", "请在出点使用精确化裁剪工具，快捷键Q");
                    return false;
                }
                return true;
            }

            function doProcess5() {
                var buildNewPa = node.tempNode.buildNewPa;
                var cname = $('input[name=categoryName]').val();
                var p_vid = $('input[name=videoName]').suggest("getValue") || 0;

                var videotype = buildNewPa.videotype;
                if (videotype == -1) {
                    p_vid = 0;
                    var bind_sel_name = $('input[name=categoryName]').attr("bind-sel-name");
                    // var bind_sel_id = $('input[name=categoryName]').attr("bind-sel-id");
                    if (bind_sel_name == null || bind_sel_name == "" || cname != bind_sel_name) {
                        WrapSys.Plugin.tips("error", '提交失败，频道不存在!');
                        return false;
                    }
                } else if (videotype == 0 || videotype == 1) {
                    var bind_sel_name = $('input[name=videoName]').attr("bind-sel-name");
                    //var bind_sel_id = $('input[name=videoName]').attr("bind-sel-id");
                    if (bind_sel_name == null || bind_sel_name == "" || buildNewPa.title != bind_sel_name) {
                        WrapSys.Plugin.tips("error", '提交失败，标题不存在!');
                        return false;
                    }
                }
                node.tempNode.p_vid = p_vid;
                node.tempNode.cname = cname;
                return true;
            }

            //final submit
            function doProcess6() {
                var buildNewPa = node.tempNode.buildNewPa;
                var videotype = buildNewPa.videotype;
                var dataSubmit = node.tempNode.dataSubmit;
                var pic = node.tempNode.pic;
                var p_vid = node.tempNode.p_vid;
                var p_albumId = $('input[name=albumName]').suggest("getValue") || "";
                var dadianmerge = {ocr_flag: 0, intelligent_insert_time: ""};
                if (WrapSys.Util.hasImportData()) {
                    dadianmerge.ocr_flag = 1;
                    dadianmerge.intelligent_insert_time = $("span[name=import-id-span]").attr("import-data-time");
                } else {
                    dadianmerge.intelligent_insert_time = WrapSys.Config.LAST_SUBMIT_TIME;
                }

                if (WrapSys.Config.fragmentPid > 0) {
                    p_vid = WrapSys.Config.fragmentPid;
                    if (!WrapSys.Util.hasImportData()) {
                        dadianmerge.intelligent_insert_time = WrapSys.Config.FIRST_FRAGMENT_START;
                    }
                }
                var fantasyTitle = $('input[name=fantasyTitle]').val() ? $('input[name=fantasyTitle]').val() : "";
                var handlerPostData = function (programId) {
                    if (videotype == -1) {
                        buildNewPa.fantasyTitle = fantasyTitle;
                    } else if (videotype == 1  ) {
                        buildNewPa.reTranscodeConfirm = WrapSys.Util.getReTranscodeConfirmVal();
                    }

                    WrapSys.Util.postData(node.settings.subData, buildNewPa, pic, programId, dataSubmit, function (flag) {
                        //提交数据给刘之红
                        if (flag) {
                            var stData = $.extend(true, {}, dataSubmit, { programId: programId, cname: node.tempNode.cname , videotype:videotype })
                            //     WrapSys.Util.doSt1Data(advertParams.delAdStr, stData, dadianmerge);
                            WrapSys.Util.doSt1Data(0, stData, dadianmerge);// 不使用 advertParams.delAdStr
                            WrapSys.Config.OCR_TITLE_FLAG = 0;
                            VODCom.AutoTitle.clearData();
                            WrapSys.Util.doSt2Data(stData);
                            //alert('打点成功');
                            WrapSys.Plugin.tips("success", '打点成功');
                            WrapSys.AccurateFrame.fgTipRemove();
                            WrapSys.AccurateFrame.adTipRemove();
                            WrapSys.Util.updateGridAdvert();
                            WrapSys.MaskManager.clearMaskEntity();
                            WrapSys.CornerManager.clearEntity();
                            WrapSys.ShimManager.clearEntity();
                            console.log("[handlerPostData] handler suc")
                            if (videotype == -1) {
                                WrapSys.Util.notifyWrapPublish(programId);
                            }
                        } else {
                            console.log("[handlerPostData] handler fail")
                        }
                    });
                }
                var dealRoute = -1;
                var wrapDelayRoute = false;
                if (WrapSys.Config.lastLegoSubmitObj.programId != WrapSys.Config.fragmentPid) {
                    WrapSys.Config.lastLegoSubmitObj.programId = WrapSys.Config.fragmentPid;
                    WrapSys.Config.lastLegoSubmitObj.hasSubmit = false;
                    dealRoute = 1;
                } else if (WrapSys.Config.lastLegoSubmitObj.hasSubmit == false) {
                    dealRoute = 2;
                } else {
                    dealRoute = 3;
                }
                if (buildNewPa.videotype == 0 || buildNewPa.videotype == 1) {
                    wrapDelayRoute = true;
                }
                var isVolumeStandard = $('input[name=isVolumeStandard]:checked').val(); //音量标准化参数
                var isVolumeStandardForMaterial = $('input[name=isVolumeStandardForMaterial]:checked').val()
                if (dealRoute == 1 || dealRoute == 2) {
                    var dataJson = {
                        'videoId': p_vid,
                        'priority': buildNewPa.priority,
                        'albumName': buildNewPa.album,
                        'videoName': buildNewPa.title,
                        'categoryId': buildNewPa.cat,
                        'albumId': p_albumId,
                        'reTranscodeConfirm': buildNewPa.reTranscodeConfirm,
                        'freeType': node.paramsData.freeType,
                        'isVolumeStandard': isVolumeStandard,
                        isVolumeStandardForMaterial: isVolumeStandardForMaterial,
                        userArea: WrapSys.Util.getUserRegion()
                    };
                    if ($("#locationDiv").is(":visible")) {
                        dataJson.locationCategory = $("input[name=location]").suggest("getValue");
                    }
                    if (videotype == -1) {
                        dataJson.fantasyTitle = fantasyTitle;
                    }
                    var sendData = {"request": JSON.stringify({url: WrapSys.Constant.thirdUrl_peditor_submit, type: "post", params: {
                        data: JSON.stringify(dataJson)
                    }, needCookie: true}), timeSign: new Date().getTime()}
                    if (wrapDelayRoute) {
                        WrapSys.Util.episodeLogSubmit(p_vid, WrapSys.Config.VOD_LOG_TYPE.delaySendDataToLego, sendData, {code: "A00000", data: {programId: p_vid}}, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                        WrapSys.Config.fragmentPid = p_vid;
                        WrapSys.Config.lastLegoSubmitObj.programId = WrapSys.Config.fragmentPid;
                        WrapSys.Config.lastLegoSubmitObj.hasSubmit = true;
                        handlerPostData(p_vid)
                    } else {
                        WrapSys.Dao.dopeditorSubmit(sendData, {
                            doSuc: function (res) {
                                if (res == null || res.code != 'A00000') {
                                    WrapSys.Util.logSubmit(WrapSys.Config.VOD_LOG_TYPE.sendProgramDataToLego, sendData, res, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                                    WrapSys.Plugin.tips("error", res == null ? "lego接口无效返回值" : "lego错误：" + res.msg);
                                    if (WrapSys.Util.isAppendFragmentMode()) {
                                        $('input[name=isAppendFragment]').removeAttr("disabled");
                                        $('input[name=isAppendFragment][value="0"]').click();
                                        //     WrapSys.Util.transToNormalMode();
                                    }
                                } else {
                                    var programId = res.data.programId;
                                    if (!programId) {
                                        WrapSys.Util.logSubmit(WrapSys.Config.VOD_LOG_TYPE.sendProgramDataToLego, sendData, {error: '乐高返回的programId为空!'}, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                                        //alert('乐高返回的programId为空!');
                                        WrapSys.Plugin.tips("error", '乐高返回的programId为空!');
                                        return false;
                                    }
                                    WrapSys.Util.episodeLogSubmit(programId, WrapSys.Config.VOD_LOG_TYPE.sendProgramDataToLego, sendData, res, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                                    WrapSys.Config.fragmentPid = programId;
                                    WrapSys.Config.lastLegoSubmitObj.programId = WrapSys.Config.fragmentPid;
                                    WrapSys.Config.lastLegoSubmitObj.hasSubmit = true;
                                    handlerPostData(programId)
                                }
                            },
                            doError: function (XMLHttpRequest, status) {
                                WrapSys.Util.logSubmit(WrapSys.Config.VOD_LOG_TYPE.sendProgramDataToLego, dataJson, { type: "error reqeust", status: status }, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                                WrapSys.Plugin.tips("error", '网络错误,请稍后再提交！');
                            }
                        })
                    }
                } else if (dealRoute == 3) {
                    handlerPostData(p_vid)
                }
            }

            function doProcess7() {
                var tmpArr = WrapSys.MaskManager.getNegativeOutpointList(node.tempNode.eTime);
                if (tmpArr.length > 0) {
                    SystemDialog.queryAlert("[遮标"+ tmpArr.join(",")+"]" + WrapSys.Config.REMIND_MSG_INFO.NO_OUTPOINT_WRAN)
                    return false;
                }
                return true
            }

            function doProcess8() {
                var tmpArr = WrapSys.CornerManager.getNegativeOutpointList( node.tempNode.eTime);
                if (tmpArr.length > 0) {
                    SystemDialog.queryAlert("[贴片"+tmpArr.join(",") +"]"+ WrapSys.Config.REMIND_MSG_INFO.NO_OUTPOINT_WRAN)
                    return false;
                }
                return true
            }

            function doSubmit() {
                node = createBaseNode();
                while (node.processArr.length > 0) {
                    var func = node.processArr.shift();
                    if (!func()) {
                        break;
                    }
                }
            }

            function keepOnSubmit() {
                if (node != null) {
                    while (node.processArr.length > 0) {
                        var func = node.processArr.shift();
                        if (!func()) {
                            break;
                        }
                    }
                }
            }

            return {
                doSubmit: doSubmit,
                keepOnSubmit: keepOnSubmit
            }
        })();
    }
);